package cn.arnold.leetcode;

/**
 * @author Arnold-Xiang
 * @Info
 */
public class IandCount {
    public static void main(String[] args) {
        char[][] grid = {{1, 1, 0}, {1, 0, 1}, {0, 0, 0}};
        int count = 0;
        int N = grid.length;
        int M = grid[0].length;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (grid[i][j] == '1') {
                    count++;
                    dfs(grid, i, j);
                }
            }
        }
        System.out.println(count);
    }

    static void dfs(char[][] grid, int i, int j) {
        grid[i][j] = 0;
        if (i - 1 >= 0 && grid[i - 1][j] == '1') {
            dfs(grid, i - 1, j);
        }
        if (i + 1 < grid.length && grid[i + 1][j] == '1') {
            dfs(grid, i + 1, j);
        }
        if (j - 1 >= 0 && grid[i][j - 1] == '1') {
            dfs(grid, i, j - 1);
        }
        if (j + 1 < grid.length && grid[i][j + 1] == '1') {
            dfs(grid, i, j + 1);
        }
    }
}
